package com.tencent.iov.cowin.befsale.clue.value.utils;

import com.google.common.collect.Lists;
import com.software.boot.common.util.EmptyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xiamu
 * @Describe: 公共方法 用于转化各种数据
 */
@Slf4j
public class CommonUtil {

    public static List<Date> getSevenDate(int dayNum) {
        List<Date> dateList = new ArrayList<>();
        for (int i = -1; i < dayNum; i++) {
            Date date = DateUtils.addDays(new Date(), +i);
            dateList.add(date);
        }
        return dateList;
    }

    public static Long getLongByObj(Object obj) {
        if (EmptyUtils.isEmpty(obj)) {
            return null;
        }
        String objStr = obj.toString();
        try {
            return Long.parseLong(objStr);
        } catch (Exception e) {
            log.info("[getLongByObj] 转化long类型失败");
        }
        return null;
    }

    public static Integer getIntegerByObj(Object obj) {
        if (EmptyUtils.isEmpty(obj)) {
            return null;
        }
        String objStr = obj.toString();
        try {
            return Integer.parseInt(objStr);
        } catch (Exception e) {
            log.info("[getIntegerByObj] 转化int类型失败");
        }
        return null;
    }

    public static List<String> getDicNameByObject(Object obj, Map<String, String> dicMap) {
        if (EmptyUtils.isEmpty(obj)) {
            return null;
        }
        String objStr = obj.toString();
        String[] codes = objStr.split(",");
        List<String> dicNames = Lists.newArrayList();
        for (String code : codes) {
            if (EmptyUtils.isNotEmpty(dicMap) && dicMap.containsKey(code)
                    && EmptyUtils.isNotEmpty(dicMap.get(code))) {
                dicNames.add(dicMap.get(code));
            }
        }
        return dicNames;
    }
}
